package com.quiz.bis.controller;

import com.quiz.bis.domain.bo.QuestionBo;
import com.quiz.bis.domain.bo.QuestionExportBo;
import com.quiz.bis.domain.model.QuestionQuery;
import com.quiz.bis.domain.vo.QuestionDetailVo;
import com.quiz.bis.domain.vo.QuestionExportVo;
import com.quiz.bis.domain.vo.QuestionVo;
import com.quiz.bis.service.IQuestionService;
import com.quiz.bis.util.pdf.ExportQueWordUtil;
import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 题目管理2
 *
 * @author ahuan
 */
@RestController
@RequestMapping("/question2")
@Tag(name = "题目管理2")
@RequiredArgsConstructor
public class QuestionController extends BaseController {
    private final IQuestionService questionService;
    private final ExportQueWordUtil exportQuestionUtil;

    /**
     * 新增题目
     *
     * @param bo 题目信息
     * @return 题目ID
     */
    @PostMapping
    public R<Long> addQuestion(@RequestBody QuestionBo bo) {
        return questionService.addQuestion(bo);
    }


    /**
     * 分页查询题目列表
     */
    @GetMapping("/list")
    public TableDataInfo<QuestionVo> list(QuestionQuery query, PageQuery page) {
        return questionService.selectPageQuestionList(query, page);
    }

    /**
     * 分页查询题目列表
     */
    @GetMapping("/list2")
    public TableDataInfo<QuestionDetailVo> list2(QuestionQuery query, PageQuery page) {
        return questionService.selectPageQuestionList2(query, page);
    }

    /**
     * 全文搜索题目
     */
    @GetMapping("/fulltext/search")
    public TableDataInfo<QuestionDetailVo> fulltextSearch(
            @RequestParam String keyword,
            @RequestParam(required = false) Long categoryId,
            PageQuery page) {
        return questionService.fulltextSearch(keyword, categoryId, page);
    }

    /**
     * 删除题目
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toR(questionService.deleteQuestionByIds(ids));
    }

    /**
     * 获取题目详情
     */
    @GetMapping("/{id}")
    public R<QuestionDetailVo> getInfo(@PathVariable Long id) {
        return R.ok(questionService.selectQuestionById(id));
    }

    /**
     * 修改题目
     */
    @PutMapping
    public R<Void> edit(@Validated @RequestBody QuestionBo bo) {
        return toR(questionService.updateQuestion(bo));
    }

    /**
     * 导出题目为PDF
     *
     * @param query    查询条件
     * @param response 响应配置
     * @param request  请求配置
     */
    @PostMapping("/export/pdf")
    public void exportPdf(@RequestBody QuestionExportBo query,
                          HttpServletResponse response,
                          HttpServletRequest request) {
        List<QuestionExportVo> questions = questionService.exportQuestionList(query);
        questionService.exportToPdf(questions, response, request, query);
    }

    /**
     * 导出题目为Word
     *
     * @param query    查询条件
     * @param response 响应配置
     * @param request  请求配置
     */
    @PostMapping("/export/word")
    public void exportWord(@RequestBody QuestionExportBo query,
                           HttpServletResponse response,
                           HttpServletRequest request) {
        List<QuestionExportVo> questions = questionService.exportQuestionList(query);
        questionService.exportToWord(questions, response, request, query);
    }

    /**
     * 导出题目为Word2
     *
     * @param query    查询条件
     * @param response 响应配置
     * @param request  请求配置
     */
    @PostMapping("/export/word2")
    public void exportWord2(@RequestBody QuestionExportBo query,
                           HttpServletResponse response,
                           HttpServletRequest request) {
        List<QuestionExportVo> questions = questionService.exportQuestionList(query);
//        questionService.exportToWord(questions, response, request, query);
        exportQuestionUtil.exportToWord(questions, response, request, query);
    }

    /**
     * 查看相似题目(根据题目ID)
     * @param questionId 题目id
     */
    @GetMapping("/get/similar/{questionId}")
    public TableDataInfo<QuestionDetailVo> getSimilarQuestions(@PathVariable Long questionId,
                                                         PageQuery page,
                                                         Double threshold) {
        return questionService.querySimilarQuestions(questionId,page,threshold);
    }

    /**
     * 查看相似题目(根据题目文本)
     * @param questionHtml 题目id
     */
    @PostMapping("/get/similar")
    public TableDataInfo<QuestionDetailVo> getSimilarQuestions(@RequestBody String questionHtml,
                                                               PageQuery page,
                                                               Double threshold) {
        return questionService.querySimilarQuestions(questionHtml,page,threshold);
    }


}